<?php


namespace App\Admin\Forms\Config;


use App\Models\Config\SystemConfig;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Illuminate\Support\Arr;

class StorageConfig extends ConfigForm implements LazyRenderable
{
    use LazyWidget;

    public static function label() :array
    {
        return [
            "cloud" => "云存储方式",
            "s3_key"=>"Key",
            "s3_secret"=>"秘钥",
            "s3_region"=>"区域",
            "s3_bucket"=>"Bucket",
            "s3_url"=>"访问域名",
            "s3_endpoint"=>"Endpoint",
            "s3_minio" => "Minio兼容",

            "qiniu_access_key" => "AccessKey",
            "qiniu_secret_key" => "SecretKey",
            "qiniu_bucket" => "Bucket",
            "qiniu_notify_url" => "回调地址",
            "qiniu_default_domains" => "七牛域名",
            "qiniu_https_domains" => "安全域名",
            "qiniu_custom_domains" => "自定义域名",
        ];
    }


    public static function mapping() :array
    {
        return [
            "cloud" => "filesystems.cloud",
            "s3_key"=>"filesystems.disks.s3.key",
            "s3_secret"=>"filesystems.disks.s3.secret",
            "s3_region"=>"filesystems.disks.s3.region",
            "s3_bucket"=>"filesystems.disks.s3.bucket",
            "s3_url"=>"filesystems.disks.s3.url",
            "s3_endpoint"=>"filesystems.disks.s3.endpoint",
            "s3_minio" => "filesystems.disks.s3.use_path_style_endpoint",

            "qiniu_access_key" => "filesystems.disks.qiniu.access_key",
            "qiniu_secret_key" => "filesystems.disks.qiniu.secret_key",
            "qiniu_bucket" => "filesystems.disks.qiniu.bucket",
            "qiniu_notify_url" => "filesystems.disks.qiniu.notify_url",
            "qiniu_default_domains" => "filesystems.disks.qiniu.domains.default",
            "qiniu_https_domains" => "filesystems.disks.qiniu.domains.https",
            "qiniu_custom_domains" => "filesystems.disks.qiniu.domains.custom",
        ];
    }
    /**
     * 构建表单.
     */
    public function form()
    {
        $label = static::label();
        $this->radio("cloud",Arr::get($label,"cloud"))->options(["s3"=>"AWS S3","qiniu"=>"QiNiu"])->default("s3")
            ->when("s3",function () use($label){
                $this->text('s3_key',Arr::get($label,"key"))->required();
                $this->password('s3_secret',Arr::get($label,"secret"))->addElementClass("eye-open")->required();
                $this->url('s3_region',Arr::get($label,"region"))->required();
                $this->text('s3_bucket',Arr::get($label,"bucket"))->required();
                $this->url('s3_url',Arr::get($label,"url"))->required();
                $this->url('s3_endpoint',Arr::get($label,"endpoint"))->required();
                $this->radio('s3_minio',Arr::get($label,"minio"))->options(["1"=>"是","0"=>"否"])->default(1)->help("使用AWS SDK for PHP来操作MinIO");
            })
            ->when("qiniu",function () use($label){
                $this->text('qiniu_access_key',Arr::get($label,"qiniu_access_key"))->required();
                $this->password('qiniu_secret_key',Arr::get($label,"qiniu_secret_key"))->required();
                $this->text('qiniu_bucket',Arr::get($label,"qiniu_bucket"))->required();
                $this->url('qiniu_notify_url',"回调地址")->required();
                $this->url('qiniu_default_domains',"七牛域名")->required();
                $this->url('qiniu_https_domains',"安全域名")->required();
                $this->url('qiniu_custom_domains',"自定义域名")->required();
            })
        ;
    }
}
